package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.hmureport.entity.HmUreportFile;
import com.harmontronics.hanma.paas.paas.dto.input.HmUreportFileCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmUreportFileUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmUreportFileResult;
import com.harmontronics.hanma.paas.paas.service.HmUreportFileAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/hmureport")
@Api(tags = {"报表"})
public class HmUreportController {

    @Autowired
    private HmUreportFileAppService hmUreportFileAppService;

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    @TokenValidSkip
    public ResponseEntity<PageBase<HmUreportFileResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
            @RequestParam(required = false) String fileName) {

        PageBase<HmUreportFile> hmTenantUserPageBase = hmUreportFileAppService.getList(pageNo, pageSize,fileName);
        PageBase<HmUreportFileResult> resultPageBase = PageBase.copy(hmTenantUserPageBase,HmUreportFileResult.class);
        return ResponseEntity.ok(resultPageBase);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmUreportFileCreateInput hmUreportFileCreateInput) {

        HmUreportFile hmUreportFile = BeanUtil.toBean(hmUreportFileCreateInput,HmUreportFile.class);
        String fileName = hmUreportFileAppService.save(hmUreportFile);
        return ResponseEntity.ok(fileName);
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmUreportFileUpdateInput hmUreportFileUpdateInput) {
        HmUreportFile hmUreportFile =BeanUtil.toBean(hmUreportFileUpdateInput,HmUreportFile.class);
        hmUreportFileAppService.update(hmUreportFile);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmUreportFileAppService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }

}
